package com.cqgc.controller;

import com.cqgc.pojo.dto.OrderDTO.OrderDTO;
import com.cqgc.pojo.dto.OrderDTO.OrderQueryDTO;
import com.cqgc.pojo.dto.OrderDTO.OrderStatusDTO;
import com.cqgc.pojo.dto.OrderDTO.OrderUpdateDTO;
import com.cqgc.pojo.entity.Order;
import com.cqgc.pojo.entity.OrderQuery;
import com.cqgc.pojo.entity.PageResult;
import com.cqgc.pojo.entity.Result;
import com.cqgc.pojo.vo.OrderDeliveryVO;
import com.cqgc.pojo.vo.OrderQuerySupplierVO;
import com.cqgc.pojo.vo.OrderQueryVO;
import com.cqgc.service.OrderService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/order")
@Api(tags = "订单接口")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 添加订单
     * @param orderDTO
     * @return
     */

    @PostMapping("/add")
    @ApiOperation("添加订单")
    @RequiresRoles("purchaser")
    public Result add(@RequestBody OrderDTO orderDTO) {
        log.info("提交订单:{}", orderDTO);
        orderService.add(orderDTO);
        return Result.success("订单生成");
    }


    /**
     * 修改订单状态
     * @param orderStatusDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改订单状态")
    @RequiresRoles(value = {"purchaser","supplier"} ,logical = Logical.OR)
    public Result updateStatus(@RequestBody OrderStatusDTO orderStatusDTO) {
        orderService.updateStatus(orderStatusDTO);
        return Result.success("修改订单状态成功");
    }


    @PostMapping("/export")
    @ApiOperation("导出为excel")
    @RequiresRoles("supplier")
    public ResponseEntity<byte[]> exportExcel(Integer status) {
        log.info("导出订单状态为{}的订单",status);
        try {

            byte[] excelContent = orderService.generateExcel(status);

            // 设置响应头，通知浏览器这是一个需要下载的文件
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
            headers.setContentDispositionFormData("attachment", "orders.xlsx"); // 文件名为orders.xlsx

            return new ResponseEntity<>(excelContent, headers, HttpStatus.OK);
        } catch (Exception e) {
            // 处理异常，可能需要更详细的错误处理逻辑
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(null);
        }
    }


    @DeleteMapping("/{orderNumber}")
    @ApiOperation("删除订单")
    @RequiresRoles(value = {"purchaser","supplier"},logical = Logical.OR)
    public Result delete(@PathVariable("orderNumber") String orderNumber){
        orderService.deleteByOrderNumber(orderNumber);
        return Result.success("删除订单成功");
    }

    @GetMapping("/findByCondition")
    @ApiOperation("采购商条件查询")
//    @RequiresRoles("purchaser")
    public Result<PageResult> findByConditionPurchaser(OrderQueryDTO orderQueryDTO) {
        log.info("查询订单:{}", orderQueryDTO);
        // 默认查询未支付的订单
//        if (orderQueryDTO.getStatus() == null) {
//            orderQueryDTO.setStatus(1);
//        }
        PageResult pageResult = orderService.findByConditionPurchaser(orderQueryDTO);
        return  Result.success(pageResult);
    }

    @GetMapping("/findByConditionSupplier")
    @ApiOperation("供应商条件查询")
//    @RequiresRoles("supplier")
    public Result<PageResult> findByConditionSupplier(OrderQueryDTO orderQuery) {

        PageResult pageResult = orderService.findByConditionSupplier(orderQuery);
        return Result.success(pageResult);
    }


    @PutMapping("/update")
    @ApiOperation("修改订单")
    @RequiresRoles("purchaser")
    public Result updateOrder(@RequestBody OrderUpdateDTO orderUpdateDTO){
        Order order = new Order();
        BeanUtils.copyProperties(orderUpdateDTO,order);
        orderService.updateOrder(order);
        return Result.success("修改订单成功");
    }
    @GetMapping("/findByOrderStatus")
    @ApiOperation("根据订单状态查询订单")
    public Result findByOrderStatus(int pageNum,int pageSize){
//        //已支付的订单
//        int status = 0;
        PageResult pageResult = orderService.findByOrderStatus(pageNum,pageSize);
        return Result.success(pageResult);
    }

}
